//
//  ICActPwdView.m
//  ICome
//
//  Created by zhangrongwu on 16/4/11.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICActPwdView.h"
#import "ICHeaderFile.h"

@implementation ICActPwdView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutSubview];
        
        WEAKSELF;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            [weakSelf endEditing:YES];
        }];
        [self addGestureRecognizer:tap];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged) name:UITextFieldTextDidChangeNotification object:self.oldPasswardTextField];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged) name:UITextFieldTextDidChangeNotification object:self.newPasswardTextField];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextChanged) name:UITextFieldTextDidChangeNotification object:self.reNewPasswardTextField];
    }
    return self;
}

- (void)textFieldTextChanged {
    self.finishButton.enabled = (_oldPasswardTextField.text.length > 0 && _newPasswardTextField.text.length > 0 && _reNewPasswardTextField.text.length > 0);
}

-(void)layoutSubview {
    CGFloat spacing = 10;
    CGFloat leftMargin = 15;
    [self.oldPasswardTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(20);
        make.left.equalTo(self.mas_left).offset(-2);
        make.right.equalTo(self.mas_right).offset(2);
        make.height.mas_equalTo(50);
    }];
    
    [self.newPasswardTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.oldPasswardTextField.mas_bottom).offset(spacing);
        make.left.equalTo(self.mas_left).offset(-2);
        make.right.equalTo(self.mas_right).offset(2);
        make.height.mas_equalTo(50);
    }];
    
    [self.reNewPasswardTextField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.newPasswardTextField.mas_bottom).offset(spacing);
        make.left.equalTo(self.mas_left).offset(-2);
        make.right.equalTo(self.mas_right).offset(2);
        make.height.mas_equalTo(50);
    }];
    
    [self.finishButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.reNewPasswardTextField.mas_bottom).offset(80);
        make.centerX.equalTo(self.mas_centerX);
        make.left.equalTo(self.mas_left).offset(leftMargin);
        make.width.mas_equalTo(App_Frame_Width - leftMargin * 2);
    }];
}

-(void)layoutSubviews {
    [super layoutSubviews];
}
-(UITextField *)oldPasswardTextField {
    if (!_oldPasswardTextField) {
        _oldPasswardTextField = [[UITextField alloc] initWithFrame:CGRectZero];
        _oldPasswardTextField.placeholder = @"旧密码";
        _oldPasswardTextField.layer.borderWidth = 1;
//        _oldPasswardTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _oldPasswardTextField.layer.borderColor = ICRGB(0xc4c4c4).CGColor;
       _oldPasswardTextField.backgroundColor = [UIColor whiteColor];
        UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
        _oldPasswardTextField.leftView = paddingView;
        _oldPasswardTextField.leftViewMode = UITextFieldViewModeAlways;
        _oldPasswardTextField.secureTextEntry = YES;
        _oldPasswardTextField.font = [UIFont systemFontOfSize:17];
        [self addSubview:_oldPasswardTextField];
    }
    return _oldPasswardTextField;
}

-(UITextField *)newPasswardTextField {
    if (!_newPasswardTextField) {
        _newPasswardTextField = [[UITextField alloc] initWithFrame:CGRectZero];
        _newPasswardTextField.placeholder = @"新密码 (6-16个字符或数字)";
        _newPasswardTextField.layer.borderWidth = 1;
        _newPasswardTextField.layer.borderColor = ICRGB(0xc4c4c4).CGColor;
        _newPasswardTextField.backgroundColor = [UIColor whiteColor];
        UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
        _newPasswardTextField.leftView = paddingView;
        _newPasswardTextField.leftViewMode = UITextFieldViewModeAlways;
        _newPasswardTextField.secureTextEntry = YES;
        _newPasswardTextField.font = [UIFont systemFontOfSize:17];
        [self addSubview:_newPasswardTextField];
    }
    return _newPasswardTextField;
}

-(UITextField *)reNewPasswardTextField {
    if (!_reNewPasswardTextField) {
        _reNewPasswardTextField = [[UITextField alloc] initWithFrame:CGRectZero];
        _reNewPasswardTextField.placeholder = @"请再次输入新密码";
        _reNewPasswardTextField.layer.borderWidth = 1;
        _reNewPasswardTextField.layer.borderColor = ICRGB(0xc4c4c4).CGColor;
        _reNewPasswardTextField.backgroundColor = [UIColor whiteColor];
        UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 20)];
        _reNewPasswardTextField.leftView = paddingView;
        _reNewPasswardTextField.leftViewMode = UITextFieldViewModeAlways;
        _reNewPasswardTextField.secureTextEntry = YES;
        _reNewPasswardTextField.font = [UIFont systemFontOfSize:17];
        [self addSubview:_reNewPasswardTextField];
    }
    return _reNewPasswardTextField;
}

-(UIButton *)finishButton {
    if (!_finishButton) {
        _finishButton = [[UIButton alloc] init];
        [_finishButton setTitle:@"确认修改" forState:UIControlStateNormal];
        _finishButton.layer.cornerRadius = 5;
        [_finishButton setBackgroundImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_detail_sender"] forState:UIControlStateNormal];
        [self addSubview:_finishButton];
        _finishButton.enabled = NO;
    }
    return _finishButton;
}
@end
